package org.example.easybuy.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.easybuy.annotation.PermissionCheck;
import org.example.easybuy.entity.Car;
import org.example.easybuy.entity.CarDetail;
import org.example.easybuy.entity.Order;
import org.example.easybuy.service.CarService;
import org.example.easybuy.util.CustomResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.mail.Address;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/car")
@RestController
public class CarController {
    ObjectMapper objectMapper=new ObjectMapper();

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CarService carService;

    @PermissionCheck("0")
    @PostMapping("/addIntoCar")
    public CustomResult addIntoCar(@RequestParam String loginName,@RequestParam String pro) throws JsonProcessingException {
        CarDetail carDetail=objectMapper.readValue(pro,CarDetail.class);
        Map<String,Object> map=carService.addIntoCar(loginName,carDetail);
        if(map.get("isAdd").equals("yes"))
        {
            return CustomResult.successResult().addResult("isAdd",true).addResult("car",map.get("car"));
        }
        return CustomResult.successResult().addResult("isAdd",false);
    }

    @PermissionCheck("0")
    @GetMapping("/getCar")
    public CustomResult getCar(String loginName) throws JsonProcessingException {

        if(stringRedisTemplate.hasKey(loginName+"_car"))
        {
            String newCarStr= stringRedisTemplate.opsForValue().get(loginName+"_car");
            Car newCar=objectMapper.readValue(newCarStr,Car.class);
            return CustomResult.successResult().addResult("car",newCar);
        }
        return CustomResult.successResult().addResult("car",null);
    }

    @PermissionCheck("0")
    @GetMapping("/deleteDetail")
    public CustomResult deleteDetail(int id,String loginName) throws JsonProcessingException {
        if(carService.deleteDetail(id,loginName))
        {
            return CustomResult.successResult().addResult("isDelete","yes");
        }
        return CustomResult.successResult().addResult("isDelete","no");
    }

    @PermissionCheck("0")
    @PostMapping("/updateCarFromRedis")
    public CustomResult updateCarFromRedis(@RequestParam Map<String,Object> map) throws JsonProcessingException {
        List<CarDetail> list=new ArrayList<>();
        list=objectMapper.readValue(map.get("detailStr").toString(),objectMapper.getTypeFactory().constructCollectionType(List.class, CarDetail.class));
        if(carService.updateCarFromRedis(list,map.get("loginName").toString()))
        {
            return CustomResult.successResult().addResult("isUpdate","yes");
        }
        return CustomResult.successResult().addResult("isUpdate","no");
    }

    @PermissionCheck("0")
    @PostMapping("/generateOrder")
    public CustomResult generateOrder(@RequestParam Map<String,Object> map){
        Order order=null;
        try {
            order=carService.generateOrder(map);
        } catch (JsonProcessingException e) {
            return CustomResult.successResult().addResult("isGene","no");
        }
        return CustomResult.successResult().addResult("isGene","yes").addResult("order",order);
    }

    @GetMapping("/checkOrderIsClose")
    public CustomResult checkOrderIsClose(String orderId)
    {
        if(stringRedisTemplate.hasKey(orderId))
        {
            return CustomResult.successResult().addResult("isClose","no");
        }
        return CustomResult.successResult().addResult("isClose","yes");
    }
}
